
package person



import org.springframework.dao.DataIntegrityViolationException

import grails.converters.JSON
import grails.converters.XML

class TelecomController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        //redirect(action: "list", params: params)
    }

    def list(Integer max) {
        params.max = Math.min(max ?: 10, 100)
		def c = Telecom.list(params)
		def cc = Telecom.count()
		println request.format;
		withFormat {
			//html {[telecomInstanceList: c, telecomInstanceTotal: cc]}
			json { render c as JSON }
			xml { render c as XML }
		}
        
    }

    def create() {
        [telecomInstance: new Telecom(params)]
    }

    def save() {
		println "Save Params = " + params;
		println "Save data = " + request.JSON
        def telecomInstance = new Telecom()
		bindData(telecomInstance ,request.JSON)
		println telecomInstance;
        if (!telecomInstance.save(flush: true)) {
            render(view: "create", model: [telecomInstance: telecomInstance])
            return
        }
		def resp = [:];
		//resp["label"] = telecom.label;
		resp["class"] = Telecom;
		resp["id"] = telecomInstance.id
		withFormat {
			//html {[telecomInstanceList: c, telecomInstanceTotal: cc]}
			json { render resp as JSON }
			xml { render resp as XML }
		}
    }

    def show(Long id) {
        def telecomInstance = Telecom.get(id)
        if (!telecomInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'telecom.label', default: 'Telecom'), id])
            redirect(action: "list")
            return
        }
		withFormat {
			html telecomInstance: telecomInstance
			json { render telecomInstance as JSON }
			xml { render telecomInstance as XML }
		}
    }

    def edit(Long id) {
        def telecomInstance = Telecom.get(id)
        if (!telecomInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'telecom.label', default: 'Telecom'), id])
            redirect(action: "list")
            return
        }

        [telecomInstance: telecomInstance]
    }

    def update(Long id, Long version) {
        def telecomInstance = Telecom.get(id)
        if (!telecomInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'telecom.label', default: 'Telecom'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (telecomInstance.version > version) {
                telecomInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'telecom.label', default: 'Telecom')] as Object[],
                          "Another user has updated this Telecom while you were editing")
                render(view: "edit", model: [telecomInstance: telecomInstance])
                return
            }
        }

        telecomInstance.properties = params

        if (!telecomInstance.save(flush: true)) {
            render(view: "edit", model: [telecomInstance: telecomInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'telecom.label', default: 'Telecom'), telecomInstance.id])
        redirect(action: "show", id: telecomInstance.id)
    }

    def delete(Long id) {
        def telecomInstance = Telecom.get(id)
        if (!telecomInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'telecom.label', default: 'Telecom'), id])
            redirect(action: "list")
            return
        }

        try {
            telecomInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'telecom.label', default: 'Telecom'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'telecom.label', default: 'Telecom'), id])
            redirect(action: "show", id: id)
        }
    }
	
}
